package net.fonline.apps;

import javax.microedition.lcdui.Graphics;
import net.fonline.apps.pathfinder.ArrayList;
import net.fonline.apps.pathfinder.Node;

/**
 * A single object of the MonsterList.
 * Contains the individual intelligence of a monster.
 * @author Michael
 */
public class Monster {

    public double x = 0;
    public double y = 0;
    private int value = 0;
    private int width = 8;
    private int height = 8;
    private double health = 1;
    private double spawnhealth = 1;
    public Monster next = null;
    private ArrayList path = null;
    private int pathpos = 0;
    private double speed = 0.0;
    private double stun_time = 0.0;

    public Monster(int health, double speed, int value) {
        this.value = value;
        spawnhealth = health;
        this.health = health;
        this.speed = speed;
        x = 86;
        y = 6;
    }

    public void move(double time) {
        if (stun_time > 0.1) {
            stun_time -= time;
            return;
        } else {
            stun_time = 0.0;
        }
        if (path == null) {
            if (LineTowerWars.ec.map.current_path == null) {
                return;
            }
            path = LineTowerWars.ec.map.current_path.pointer();
            pathpos = 1;
        }
        // get the current (next) node of the path
        if (path.size() - pathpos < 0) {
            LineTowerWars.ec.lives--;
            health = -1;
            LineTowerWars.ec.map.monsters.remove(this);
            return;
        }
        Node node = path.get(path.size() - pathpos);
        double mov_x = (node.square.x * 20) - x + 6;
        double mov_y = (node.square.y * 20) - y + 6;
        double normal = Math.sqrt((mov_x * mov_x) + (mov_y * mov_y));
        if ((mov_x / normal * time / 20 * speed) * (mov_x / normal * time / 20 * speed) < mov_x * mov_x) {
            x += mov_x / normal * time / 20 * speed;
        } else {
            x += mov_x;
        }
        if ((mov_y / normal * time / 20 * speed) * (mov_y / normal * time / 20 * speed) < mov_y * mov_y) {
            y += mov_y / normal * time / 20 * speed;
        } else {
            y += mov_y;
        }

        if (x == node.square.x * 20 + 6 && y == node.square.y * 20 + 6) {
            pathpos++;
        }
    }

    public void update(Graphics g) {
        // draw a rect
        g.setColor(255 - (int) (health / spawnhealth * 255), (int) (health / spawnhealth * 255), 0);
        g.fillArc((int) (this.x * LineTowerWars.ec.scale) + LineTowerWars.ec.relX, (int) (this.y * LineTowerWars.ec.scale) + LineTowerWars.ec.relY, (int) (this.width * LineTowerWars.ec.scale), (int) (this.height * LineTowerWars.ec.scale), 0, 360);
    }

    void damage(int damage_type, int damage_power) {
        if (damage_type == 0) {
            if (stun_time < damage_power) {
                stun_time = damage_power;
            }
        } else {
            health -= damage_power;
        }
        if (health < 0) {
            LineTowerWars.ec.coins += value;
            LineTowerWars.ec.map.monsters.remove(this);
        }
    }

    boolean isDead() {
        return health < 0;
    }
}
